package com.yx.shop.web.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yx.shop.common.Const;
import com.yx.shop.entitly.Type;
import com.yx.shop.service.admin.AdminTypeService;
import com.yx.shop.service.admin.impl.AdminTypeServiceImpl;
import com.yx.shop.web.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Classname TypeController
 * @Description TODO:
 * @Date 2021/05/07 11:46
 * @Created by yx
 */
@WebServlet("/admin/type")
public class AdminTypeController extends BaseServlet {
    private AdminTypeService adminTypeService = new AdminTypeServiceImpl();

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/07 11:44
     * @Description:获取货物品类
     **/
    public String adminGoodType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Type> typeList = adminTypeService.getAllType();
        request.setAttribute("goodsTypeList", typeList);

        return Const.FORWORD + "/admin/showGoodsType.jsp";
    }

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/10 18:48
     * @param request
     * @param response
     * @Description:获取商品分类--->用于新增商品分类
     **/
    public String addGoodsType(HttpServletRequest request, HttpServletResponse response){
        List<Type> typeList = adminTypeService.getAllType();
        request.setAttribute("goodsTypeList", typeList);

        return Const.FORWORD + "/admin/addGoodsType.jsp";
    }

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/10 19:09
     * @param request
     * @param response
     * @Description:新增type
     **/
    public String insertGoodsType(HttpServletRequest request, HttpServletResponse response){
        //1.获取参数
        String tname = request.getParameter("tname");
        String tinfo = request.getParameter("tinfo");
        //2.调用业务
        Type type = new Type(tname,tinfo);
        adminTypeService.insertType(type);
        //3.重定向--->重新查询商品分类
        return Const.REDIRECT + "/admin/type?method=adminGoodType";
    }

    public String deleteType(HttpServletRequest request,HttpServletResponse response){
        //1.获取参数
        Integer tid = Integer.valueOf(request.getParameter("tid"));
        adminTypeService.deleteTypeByTid(tid);


        return Const.REDIRECT + "/admin/type?method=adminGoodType";
    }

    public String getGoodType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*List<Type> typeList = adminTypeService.getAllType();
        //转为json
        ObjectMapper objectMapper = new ObjectMapper();
        String typeString = objectMapper.writeValueAsString(typeList);
        return typeString;*/

        List<Type> typeList = adminTypeService.getAllType();
        request.setAttribute("TypeList", typeList);

        return Const.FORWORD + "/admin/addGoods.jsp";
    }
}
